<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Umum extends MX_Controller {
    
  function __construct() {      
    
    $this->load->library('template');
    $this->load->library('global_models');
    $this->load->library('session');
    
//    $this->debug($this->menu, true);
  }
  public function set_service($tanggal_fix, $pos, $service, $belanja, $service_belanja, $p_service, $p_belanja, $p_service_plus, $p_belanja_plus, $total_hpp){
    if(
      $tanggal_fix AND 
      $pos AND 
      $service AND 
      $belanja AND 
      $service_belanja AND 
      $p_service AND 
      $p_belanja AND 
      $p_service_plus AND 
      $p_belanja_plus AND 
      $total_hpp){
      
      $tanggal_fix = str_replace("_", "-", $tanggal_fix);
      $data_outlet = $this->global_models->get_query("
        SELECT *
        FROM m_outlet
        WHERE pos = '{$pos}' AND last_console IS NOT NULL AND id_status_outlet = 0
        ");

      $id_outlet = $data_outlet[0]->id_outlet;


      $tgl = strtotime($tanggal_fix);
      $date_bulan =  date("n", $tgl);
      $date_year =  date("Y", $tgl);
      $date_day =  date("j", $tgl);

      $this->cek_actifity($id_outlet, date("m", $tgl), $date_year);

      $data_report =  $this->global_models->get("m_actifity_report", array("id_outlet" => $id_outlet, "month" => $date_bulan, 'year' => $date_year));

      $id_actifity_report = $data_report[0]->id_actifity_report;
      $day_actifity_report =  $this->global_models->get("d_day_actifity_report", array("id_actifity_report" => $id_actifity_report, "tanggal" => $date_day));
      $id_day_actifity_report = $day_actifity_report[0]->id_day_actifity_report;

      if(empty($id_day_actifity_report)){
        $kirim = array(
            "service"               =>  $service,
            "belanja"               =>  $belanja,
            "service_belanja"       =>  $service_belanja,
            "p_service"             =>  $p_service,
            "p_belanja"             =>  $p_belanja,
            "p_service_plus"        => $p_service_plus,
            "p_belanja_plus"        =>  $p_belanja_plus,
            "total_hpp"             => $total_hpp,
            "id_actifity_report"    => $id_actifity_report,
            "tanggal"               =>  $date_day,
            "create_by_users"       =>  $this->session->userdata('id'),
            "create_date"           =>  date("Y-m-d H:i:s"),
            "update_by_users"       =>  $this->session->userdata('id')
          );
        $this->global_models->insert("d_day_actifity_report", $kirim);
        $pesan = "insert";
       }else{
         $kirim = array(
            "service"               =>  $service,
             "belanja"               =>  $belanja,
             "service_belanja"       =>  $service_belanja,
             "p_service"             =>  $p_service,
             "p_belanja"             =>  $p_belanja,
             "p_service_plus"        => $p_service_plus,
             "p_belanja_plus"        =>  $p_belanja_plus,
             "total_hpp"             => $total_hpp,
             "id_actifity_report"    => $id_actifity_report,
             "tanggal"               =>  $date_day,
             "update_by_users"       =>  $this->session->userdata('id')
           );

         $hasil_in = $this->global_models->update("d_day_actifity_report", array("id_day_actifity_report" => $id_day_actifity_report), $kirim);
       }
       $this->global_models->update("m_outlet", array("id_outlet" => $id_outlet), array("last_console" => $tanggal_fix));
       print 1;die;
    }
    else{
      print 0;die;
    }
    die;
	} 
  
  private function cek_actifity($id, $date_bulan, $date_year){
  $bulan = (int) $date_bulan;
    if($this->global_models->get_field("m_actifity_report", "id_actifity_report", 
            array("month"  =>  $bulan, "year" => $date_year, "id_outlet" => $id)) === FALSE){
      $this->global_models->insert("m_actifity_report", 
              array(
                  "id_outlet" =>  $id,
                  "name"      =>  "Laporan ".date("F")." ".date("Y"),
                  "month"     =>  $date_bulan,
                  "day_in_month"     =>  date("t", strtotime("{$date_year}-{$date_bulan}-01")),
                  "year"      =>  $date_year,
                  "create_by_users" =>  1,
                  "create_date" =>  date("Y-m-d H:i:s"),
                  "update_by_users" =>  1
                  ));
    }
    return true;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */